﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Game1 : MonoBehaviour 
{

    #region Others
    public EndTip endTip;
    public GongReng gongRen;
    public CameraPathBezierAnimator cameraPathAni;
    public GameObject curser;
    public Transform cameraRig;
    public GameObject ground;
    //楼层上的地面
    public GameObject floor;
    private bool isStartRotate = false;
    TriggerEvent triggerEnvent;
    public MyCameraEffect cameraEffect;
    public AudioClip audioClip;
    public InputController leftController;
    public InputController rightController;
    #endregion

    void Awake()
    {
        //
        PlayerPrefs.SetString("sceneName", "Scene1");
        Rigidbody rig= cameraRig.gameObject.AddComponent<Rigidbody>();
        rig.useGravity = false;
        BoxCollider box = cameraRig.gameObject.AddComponent<BoxCollider>();
        box.size = new Vector3(1.3f, 2f, 1.3f);
        box.center = new Vector3(0, 1.9f, 0);

        triggerEnvent = cameraRig.GetComponent<TriggerEvent>();
        triggerEnvent.targertEntered = ground;
        ground.GetComponent<BoxCollider>().enabled = false;

        BoxCollider boxTrigger = cameraRig.gameObject.AddComponent<BoxCollider>();
        boxTrigger.isTrigger = true;
        

    }
    void Start()
    {
        curser.GetComponent<DistanceEvent>().OnNearEvent.AddListener(StartPlay);
        triggerEnvent.onOtherEnterEvent.AddListener(PlayFallDownSound);

        cameraEffect.OnCancleBlack += OnEnd;
        gongRen.onHitGround += GongRen_onHitGround;
        //开始
        CheckMode();
        SoundManager.instance.PlayBGMusic(audioClip);
    }

    private void GongRen_onHitGround()
    {

        Invoke("OnEnd",3f);
    }

    private void OnEnd()
    {
        leftController.DisAbleTeleProt();
        rightController.DisAbleTeleProt();
        MyControllerManager.instance.EnableController();
        endTip.ShowTip(TipWords.game1_gaochuzhuiluo);
    }

    void Update()
    {
        if (isStartRotate == true)
        {
            cameraRig.Rotate(Vector3.right, 2f);
        }
    }

    private void CheckMode()
    {
        if (PlayerPrefs.GetString("mode") == "教育模式")
        {
            Debug.Log("开始教育模式");
            Debug.Log("当前场景为：" + PlayerPrefs.GetString("sceneName"));
            cameraPathAni.Play();
            MyControllerManager.instance.DisableController();
        }
        else
        {
            Debug.Log("开始体验模式");
            Debug.Log("当前场景为：" + PlayerPrefs.GetString("sceneName"));
            gongRen.gameObject.SetActive(false);
        }
    }




    void StartPlay()
    {
        leftController.DisAbleTeleProt();
        rightController.DisAbleTeleProt();
        curser.SetActive(false);
        if (PlayerPrefs.GetString("mode") == "教育模式")
        {
            gongRen.Play();
           
        }
        else
        {           
            //三秒后开始坠落
            Invoke("StartFallDown", 3f);
        }

    }

    /// <summary>
    /// 摄像机开始坠落
    /// </summary>
    void StartFallDown()
    {
       
        BoxCollider boxCollider = cameraRig.GetComponent<BoxCollider>();
        boxCollider.enabled = true;
        
        Rigidbody rig = cameraRig.GetComponent<Rigidbody>();
        rig.useGravity = true;
        rig.collisionDetectionMode = CollisionDetectionMode.Continuous;
        
        floor.GetComponent<BoxCollider>().enabled = false;  
             
        CameraRotate(true);
        SoundManager.instance.Play("jianjiaosheng_Man");
        ground.GetComponent<BoxCollider>().enabled = true;
        Invoke("SetCollider",3f);
    }

    /// <summary>
    /// 触发器接触地面播放落地声
    /// </summary>
    public void PlayFallDownSound()
    {
        cameraEffect.PlayDeadBlood();
        SoundManager.instance.Play("luodigangguan");
        isStartRotate = false;
        Invoke("FallDownCamera", 3f);
        //MyControllerManager.instance.EnableController();
    }

    public void FallDownCamera()
    {
        print("----FallDown");
        //UserHelper.FallDown(cameraRig);
    }
    private void SetCollider()
    {
        ground.GetComponent<BoxCollider>().enabled = true;
    }
    /// <summary>
    /// 摄像机旋转
    /// </summary>
    void CameraRotate(bool isRotate)
    {
        Debug.Log("摄像机开始旋转");
        isStartRotate = isRotate;
    }


}
